﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Submarines
{
    public partial class frmTemp : Form
    {
        Remote _remoteUser;
        TableLayoutPanelCellPosition random_tlpCell = new TableLayoutPanelCellPosition();
        static Random _rand1 = new Random();
        static Random _rand2 = new Random();
        string vector = "V";
        int count = 1;        
        public frmTemp()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lstAvailableShips.SelectedIndex = 0;
            btnShipToBeAdded.Name = "Ship" + lstAvailableShips.SelectedItem.ToString().Substring(0, 1) + vector;
            SetRemoteDropDown(false);
            OnClickDisabled();
            lblInfo.Text = "Locate ships on left Panel and press START";
            _remoteUser = new Remote((TableLayoutPanel)ctrlLocalUser.Controls["tblPanel_My"]);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox tempLst = (ListBox)sender;
            if (tempLst != null)
            {
                if (tempLst.SelectedItem != null)
                {
                    int ship_height = 0;
                    int ship_width = 0;
                    if (chkShipRotate.Checked)
                    {
                        vector = "H";
                        ship_height = 45;
                        ship_width = int.Parse(lstAvailableShips.SelectedItem.ToString().Substring(0, 1)) * 45;
                    }
                    else
                    {
                        vector = "V";
                        ship_height = int.Parse(lstAvailableShips.SelectedItem.ToString().Substring(0, 1)) * 45;
                        ship_width = 45;
                    }
                    btnShipToBeAdded.Name = "Ship" + lstAvailableShips.SelectedItem.ToString().Substring(0, 1) + vector;
                    btnShipToBeAdded.Height = ship_height;
                    btnShipToBeAdded.Width = ship_width;
                }
            }
            else
            {
                if (chkShipRotate.Checked)
                    btnShipToBeAdded.Name= btnShipToBeAdded.Name.Replace("V", "H");
                else
                    btnShipToBeAdded.Name =btnShipToBeAdded.Name.Replace("H", "V");
                //btnShipToBeAdded.Name = btnShipToBeAdded.Name.Replace("V", "H");
            }
        }
      
        private void chkShipRotate_CheckedChanged(object sender, EventArgs e)
        {
            int temp = btnShipToBeAdded.Height;
            btnShipToBeAdded.Height = btnShipToBeAdded.Width;
            btnShipToBeAdded.Width = temp;
            listBox1_SelectedIndexChanged(null, null);
        }

        private void btnShipToBeAdded_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                btnShipToBeAdded.DoDragDrop(sender, DragDropEffects.Copy);
                if (Global._added_ok)
                {
                    lstAvailableShips.Items.Remove(lstAvailableShips.SelectedItem);
                    try
                    {
                        lstAvailableShips.SelectedIndex = 0;
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        btnShipToBeAdded.Visible = false;
                    }
                    lstAvailableShips.Refresh();
                    Global._added_ok = false;
                }
            }
        }

        void GenerateRemoteShips()
        {
            int col, row;
            bool hor;
            SetRemoteDropDown(true);
            while (lstRemote.Items.Count != 0)
            {
                vector = RandomVector();
                btnRemote.Name = "Ship" + lstRemote.Items[0].ToString().Substring(0, 1) + RandomVector();
                hor = btnRemote.Name.Substring(5, 1).Equals("H");
                col = GetRandomNumber(1);
                row = GetRandomNumber(2);
                if (ctrlRemote.DropOk(new TableLayoutPanelCellPosition(col, row), int.Parse(btnRemote.Name.Substring(4, 1)), hor))
                {
                    ctrlRemote.AddRemoteShip(btnRemote, col, row, false);
                    lstRemote.Items.Remove(lstRemote.Items[0]);
                }
            }
        }

        void GenerateMe()
        {
          
        }

        void SetRemoteDropDown(bool status)
        {
            foreach (Control c in ctrlRemote.Controls)
            {
                TableLayoutPanel tlp = c as TableLayoutPanel;
                if (tlp != null)
                foreach (Control tc in tlp.Controls)
                {
                    tc.AllowDrop = status;
                }
            }
        }

        void OnClickDisabled()
        {
            Button btn;
            foreach (Control c in ctrlLocalUser.Controls)
            {
                TableLayoutPanel tlp = c as TableLayoutPanel;
                if (tlp != null)
                foreach (Control tc in tlp.Controls)
                {
                    btn = (Button)tc;
                    
                    btn.Click -= new EventHandler(ctrlLocalUser.tempButton_Click);
                }
            }
        }

        int GetRandomNumber(int num)
        {
            switch (num)
            {
                case 1:
                    return _rand1.Next(0, 99)%10;
//                    break;
                case 2:
                    return _rand2.Next(0, 99)%10;
  //                  break;
            }
            
            return _rand1.Next(0,9);
        }

        string RandomVector()
        {
            int i=_rand1.Next(0, 99);
            if (i%2 ==  0)
                return "V";
            else
                return "H";
        }

      

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (lstAvailableShips.Items.Count == 0)
            {
                GenerateRemoteShips();
                Global._user_turn = true;
                lblInfo.Text = "Your turn";
                timer1.Start();
            }
            else
            {
                MessageBox.Show("Please locate all of your ships on user panel");
            }

        }

        private void btnGetMeTurn_Click(object sender, EventArgs e)
        {
            Global._user_turn = true;
        }

        //private void timer1_Tick(object sender, EventArgs e)
        //{
        //    Button btn;
        //    //TableLayoutPanel tblRemote = (TableLayoutPanel)ctrlLocalUser.Controls["tblPanel_My"];
        //    TableLayoutPanelCellPosition tlpCell;
        //    if (!Global._user_turn)
        //    {
        //        tlpCell = _remoteUser.GetCell();
        //        btn = (Button)_remoteUser._remoteTable.GetControlFromPosition(tlpCell.Column, tlpCell.Row);
        //        if (ctrlRemote.Hit(btn))
        //        {
        //            lblUser.Text = ctrlRemote._hited.ToString();
        //            lblPC.Text = ctrlLocalUser._hited.ToString();
        //            Global._user_turn = false;
        //            if (ctrlRemote._hited == 20)
        //            {
        //                MessageBox.Show("Computer WINS");
        //                timer1.Stop();
        //            }
        //            _remoteUser.MarkCellsAroundHitted();
        //        }
        //        else
        //        {
        //            Global._user_turn = true;
        //        }
        //    }
        //}

        private void timer1_Tick(object sender, EventArgs e)
        {
            Button btn;
            //TableLayoutPanel tblRemote = (TableLayoutPanel)ctrlLocalUser.Controls["tblPanel_My"];
            //TableLayoutPanelCellPosition tlpCell;
            
            if (!Global._user_turn)
            {
                if (_remoteUser._prev_hited == false)
                {
                    random_tlpCell = _remoteUser.GetRabdomCell();
                    btn = (Button)_remoteUser._remoteTable.GetControlFromPosition(random_tlpCell.Column, random_tlpCell.Row);
                    _remoteUser._random_hited = ctrlLocalUser.Hit(btn);
                    //_remoteUser._prev_hited = _remoteUser._random_hited;
                    if (_remoteUser._random_hited)
                    {
                        _remoteUser.FillVectors();
                        _remoteUser._prev_hited = true;
                        _remoteUser.MarkCellsWhenHitted(random_tlpCell);
                        ctrlLocalUser.ShowMissedAround(random_tlpCell.Column,random_tlpCell.Row);
                        ContinueHit(random_tlpCell);

                    }
                    else
                    {
                        _remoteUser.MarkMissed(random_tlpCell);
                        Global._user_turn = true;
                        lblInfo.Text = "Your turn";

                    }
                }
                else
                {
                    ContinueHit(random_tlpCell);
                }





               






              
            }
        }

        private void ContinueHit(TableLayoutPanelCellPosition cell)
        {
            bool hited = false;
            Button btn;
            TableLayoutPanelCellPosition c_cell;
            c_cell = _remoteUser.GetNexCellByHitted(cell,true);
            btn = _remoteUser.GetButtonByCell(c_cell);
            hited = ctrlLocalUser.Hit(btn);
            if (hited)
            {
                while (hited && count < 4)
                {
                    count++;
                    _remoteUser.MarkCellsWhenHitted(c_cell);
                    ctrlLocalUser.ShowMissedAround(c_cell.Column, c_cell.Row);
                    //if (_remoteUser._prev_hited)
                    //{
                        c_cell = _remoteUser.GetNexCellByHitted(c_cell, false);
                        if (_remoteUser.CheckCellToHit(c_cell))
                        {
                            btn = _remoteUser.GetButtonByCell(c_cell);
                            hited = ctrlLocalUser.Hit(btn);



                            if (!hited)
                            {
                                switch (_remoteUser._vector)
                                {
                                    case 1:
                                        _remoteUser._vector = 3;
                                        break;
                                    case 2:
                                        _remoteUser._vector = 4;
                                        break;
                                    case 3:
                                        _remoteUser._vector = 1;
                                        break;
                                    case 4:
                                        _remoteUser._vector = 2;
                                        break;
                                }
                                Global._user_turn = true;

                            }
                            else
                            {
                                ctrlLocalUser.ShowMissedAround(c_cell.Column, c_cell.Row);
                                count++;
                            }
                            if (count == 4)
                            {
                                _remoteUser._prev_hited = false;
                                count = 0;
                            }

                        }
                        else
                        {
                            hited = false;
                        }
                    //}
                }
                if (_remoteUser._vectors.Count == 0)
                    _remoteUser._prev_hited = false;

            }
            else
            {
                _remoteUser.MarkMissed(c_cell);
            }

        }

        private void btnRemoteMove_Click(object sender, EventArgs e)
        {
            timer1_Tick(null, null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int col, row;
            bool hor;
            foreach (Control c in ctrlLocalUser.Controls)
            {
                TableLayoutPanel tlp = c as TableLayoutPanel;
                if (tlp != null)
                    foreach (Control tc in tlp.Controls)
                    {
                        tc.AllowDrop = true; ;
                    }
            }
            //SetRemoteDropDown(true);
            while (lstAvailableShips.Items.Count != 0)
            {
                vector = RandomVector();
                btnShipToBeAdded.Name = "Ship" + lstAvailableShips.Items[0].ToString().Substring(0, 1) + RandomVector();
                hor = btnShipToBeAdded.Name.Substring(5, 1).Equals("H");
                col = GetRandomNumber(1);
                row = GetRandomNumber(2);
                if (ctrlLocalUser.DropOk(new TableLayoutPanelCellPosition(col, row), int.Parse(btnShipToBeAdded.Name.Substring(4, 1)), hor))
                {
                   
                    ctrlLocalUser.AddRemoteShip(btnShipToBeAdded, col, row,true);
                    lstAvailableShips.Items.Remove(lstAvailableShips.Items[0]);
                }
            }
        }
    }
}
